package com.uni.db;

import java.util.List;

import org.hibernate.criterion.Restrictions;

import com.uni.domain.File;

public class FileServiceImpl extends GenericServiceImpl<File> implements FileService {

	@SuppressWarnings("unchecked")
	@Override
	public File getFileByName(String path, String name) {
		List<File> c = getSession().createCriteria(File.class)
		.add(Restrictions.eq("name", name))
		.add(Restrictions.eq("path", path))
		.list();
		if(c.size() == 0) return null;
		return c.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void saveFile(File file) {
		List<File> files = dao.getSession().createCriteria(File.class)
		.add(Restrictions.eq("name", file.getName()))
		.add(Restrictions.eq("path", file.getPath()))
		.list();
		for(File f : files){
			dao.delete(f);
		}
		dao.save(file);
	}

}
